package com.hzlx.setest.service.impl;

import java.util.List;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hzlx.setest.mapper.InventoryMapper;
import com.hzlx.setest.domain.Inventory;
import com.hzlx.setest.service.IInventoryService;

/**
 * 库存管理Service业务层处理
 * 
 * @author zz
 * @date 2025-07-28
 */
@Service
public class InventoryServiceImpl implements IInventoryService 
{
    @Autowired
    private InventoryMapper inventoryMapper;
    @SentinelResource(value="selectInventoryById", blockHandler = "selectInventoryByIdBlockHandler", fallback = "selectInventoryByIdFallback")
    /**
     * 查询库存管理
     * 
     * @param id 库存管理主键
     * @return 库存管理
     */
    @Override
    public AjaxResult selectInventoryById(Long id)
    {
        if(id==1){ // 业务异常触发降级
            throw new RuntimeException("服务异常！");  // 触发应急处理
        }
        if(id==2){ // 平均响应时间超时触发熔断
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        return AjaxResult.success(inventoryMapper.selectInventoryById(id));
    }
    public AjaxResult selectInventoryByIdBlockHandler(Long id, BlockException blockException){
        return AjaxResult.error("熔断降级");
    }

    public AjaxResult selectInventoryByIdFallback(Long id, Throwable throwable){
        return AjaxResult.error("业务异常，应急处理");
    }
    /**
     * 查询库存管理列表
     * 
     * @param inventory 库存管理
     * @return 库存管理
     */
    @Override
    public List<Inventory> selectInventoryList(Inventory inventory)
    {
        return inventoryMapper.selectInventoryList(inventory);
    }

    /**
     * 新增库存管理
     * 
     * @param inventory 库存管理
     * @return 结果
     */
    @Override
    public int insertInventory(Inventory inventory)
    {
        return inventoryMapper.insertInventory(inventory);
    }

    /**
     * 修改库存管理
     * 
     * @param inventory 库存管理
     * @return 结果
     */
    @Override
    public int updateInventory(Inventory inventory)
    {
        return inventoryMapper.updateInventory(inventory);
    }

    /**
     * 批量删除库存管理
     * 
     * @param ids 需要删除的库存管理主键
     * @return 结果
     */
    @Override
    public int deleteInventoryByIds(Long[] ids)
    {
        return inventoryMapper.deleteInventoryByIds(ids);
    }

    /**
     * 删除库存管理信息
     * 
     * @param id 库存管理主键
     * @return 结果
     */
    @Override
    public int deleteInventoryById(Long id)
    {
        return inventoryMapper.deleteInventoryById(id);
    }
}
